package cn.bxshey.systemLogInfo.listern;

import cn.bxshey.mq.constant.MQ;
import cn.bxshey.systemLogInfo.mapper.BabyLogMapper;
import cn.bxshey.systemLogInfo.pojo.BabyLog;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @作者：不想说话而已
 * @Date：2023/12/6 15:12
 *
 * 日志监听
 */

@Component
@RefreshScope(proxyMode = ScopedProxyMode.DEFAULT)
public class LogInfoLister {


     @Resource
     BabyLogMapper babyLogMapper;


    //监听 正常使用的日志队列
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQ.QUEUE_LOG_INFO),//队列名
            exchange = @Exchange(name = MQ.EXCHANGE_LOG, type = ExchangeTypes.TOPIC), // 交换机名称、类型
            key = {MQ.KEY_LOG_INFO} //监听的规则
    ))
    public void logInfo(Map<String,String> info) throws ParseException {
        System.err.println("收到消息日志");
        BabyLog babyLog = new BabyLog();
        babyLog.setIp( info.get("ip"));
        babyLog.setArgs(info.get("args"));
        babyLog.setCode(Long.valueOf(info.get("code")));
        String time = String.valueOf(info.get("create_time"));
        SimpleDateFormat sdf =   new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
        Date date = sdf.parse( time );
        babyLog.setCreateTime(date);
        babyLog.setMsg(info.get("msg"));
        babyLog.setRequestMethod(info.get("request_method"));
        babyLog.setRequestUrl(info.get("request_url"));
        babyLog.setPackageName(info.get("package_name"));
        babyLog.setTake(Integer.valueOf(info.get("take")));
        babyLog.setIpCity(info.get("ip_city"));
        babyLog.setResultData(info.get("result_data"));
        babyLog.setError(info.get("error"));
        babyLog.setServe(Integer.valueOf(info.get("serve")));
      try {
          babyLogMapper.insertOne(babyLog);
      }catch (Exception e){
          e.printStackTrace();
      }
    }



    //监听 错误日志队列
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQ.QUEUE_LOG_ERROR), //队列名
            exchange = @Exchange(name = MQ.EXCHANGE_LOG, type = ExchangeTypes.TOPIC), // 交换机名称、类型
            key = {MQ.KEY_LOG_ERROR} //监听的规则
    ))
    public void logError(Map<String,String> info){
        System.err.println("收到消息日志");
        System.err.println(info);
//        System.err.println("2----->"+app);


    }




    //监听 错误信息队列
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQ.QUEUE_ERROR), //惰性队列的使用
            exchange = @Exchange(name = MQ.EXCHANGE_ERROR), // 交换机名称、类型
            key = {MQ.KEY_ERROR} //监听的规则
    ))
    public <T> void logInfo2(T msg){
        System.err.println("收到错误消息日志-----------------》");
        System.err.println(msg);

    }
}
